package com.timer;

import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
	Timer mTimer;

	public static void main(String[] args) throws InterruptedException {
		TimerDemo demo=new TimerDemo();
		demo.change();
		Thread.sleep(5000);
		demo.change();
		
	}
	public void change() {
		if (mTimer == null) {
			mTimer = new Timer();
			System.out.println("==null");

		} else {
			mTimer.cancel();
			System.out.println("cancel");
		}
		mTimer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.println(i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		} , 1000);

	}

}
